﻿using UnityEngine;
using System.Collections;

namespace edu.wpi.cs.Project1
{
    /// <summary>
    /// The behaviour of the game ball.
    /// </summary>
    public class BallBehaviour : MonoBehaviour
    {
        /// <summary>
        /// The main script.
        /// </summary>
        private ARPhysicsBehaviour physicsBehaviour;

        /// <summary>
        /// The name of the last collided game collider object as a workaround
        /// for adding many points to the score instead of just one.
        /// </summary>
        private string lastColliderName;

        /// <summary>
        /// Initializes the ball.
        /// </summary>
        void Start()
        {
            physicsBehaviour = Camera.main.GetComponent<ARPhysicsBehaviour>();
            lastColliderName = string.Empty;
        }

        /// <summary>
        /// Updates the ball per frame.
        /// </summary>
        void Update()
        {

        }

        /// <summary>
        /// Called when a new collision was detected
        /// </summary>
        /// <param name="other">The other collider</param>
        void OnCollisionEnter(Collision collision)
        {
            GameObject other = collision.collider.gameObject;

            if (other.tag.Equals("Finish"))
            {
                physicsBehaviour.AddScore(10);
                physicsBehaviour.StartRound();
                Destroy(this.gameObject);
            }
            else if (other.tag.Equals("GameCollider"))
            {
                if (!other.name.Equals(lastColliderName))
                {
                    physicsBehaviour.AddScore(1);
                    lastColliderName = other.name;
                }
            }
        }
    }
}
